package com.csi.bumblebee.controller.backstage;

import com.csi.bumblebee.domain.dictionary.ExamType;
import com.csi.bumblebee.domain.dictionary.JobExam;
import com.csi.bumblebee.domain.dictionary.JobType;
import com.csi.bumblebee.domain.dictionary.SkillType;
import com.csi.bumblebee.domain.util.MyResult;
import com.csi.bumblebee.domain.util.StateCode;
import com.csi.bumblebee.service.ExamTypeService;
import com.csi.bumblebee.service.JobExamService;
import com.csi.bumblebee.service.JobTypeService;
import com.csi.bumblebee.service.SkillTypeService;
import com.csi.bumblebee.service.impl.JobExamServiceImpl;
import com.csi.bumblebee.util.ExcelReader;
import com.csi.bumblebee.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.csi.bumblebee.util.UploadUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 孙伟俊
 */
@RestController
@RequestMapping("/backstage/jobExam")
public class BackStageJobExamController {

    @Autowired
    private JobTypeService jobTypeService;

    @Autowired
    private SkillTypeService skillTypeService;

    @Autowired
    private ExamTypeService examTypeService;

    @Autowired
    private JobExamServiceImpl jobExamServiceImpl;

    /**
     * 展示所有的岗位类型
     * @return MyResult<List<JobType>>
     */
    @GetMapping("/jobType")
    public MyResult<List<JobType>> listAllJobType(){

        MyResult<List<JobType>> myResult = new MyResult<>(StateCode.FIND_SUCCESS, "查询成功");
        List<JobType> jobTypeList = jobTypeService.listJobTypeTree();

        if (jobTypeList != null) {
            myResult.setData(jobTypeList);
            return myResult;
        }else {
            myResult.setCode(StateCode.FIND_FAILURE);
            myResult.setMessage("查询失败");
        }

        return myResult;
    }

    /**
     * 后台显示所有技能类型
     * @return MyResult<List<SkillType>>
     */
    @GetMapping("/skillType")
    public MyResult<List<SkillType>> listSkillType(){
        MyResult<List<SkillType>> myResult = new MyResult<>(StateCode.FIND_SUCCESS, "查询成功");
        List<SkillType> skillTypeList = skillTypeService.list();

        if (skillTypeList != null) {
            myResult.setData(skillTypeList);
            return myResult;
        }else {
            myResult.setCode(StateCode.FIND_FAILURE);
            myResult.setMessage("查询失败");
        }

        return myResult;
    }

    /**
     * 后台显示所有题目类型
     * @return MyResult<List<ExamType>>
     */
    @GetMapping("/examType")
    public MyResult<List<ExamType>> listExamType(){
        MyResult<List<ExamType>> myResult = new MyResult<>(StateCode.FIND_SUCCESS, "查询成功");
        List<ExamType> examTypeList = examTypeService.list();

        if (examTypeList != null) {
            myResult.setData(examTypeList );
            return myResult;
        }else {
            myResult.setCode(StateCode.FIND_FAILURE);
            myResult.setMessage("查询失败");
        }

        return myResult;
    }

    /**
     * 后台增加题目
     * @return MyResult<Integer>
     */
    @PostMapping("/jobExam")
    public MyResult<Integer> addJobExam(@RequestBody JobExam jobExam){

        jobExam.setId(IdUtil.getString(JobExam.PRE_NAME));

        MyResult<Integer> myResult = new MyResult<>(StateCode.INSERT_SUCCESS,"增加成功");

        int result = jobExamServiceImpl.addJobExam(jobExam);

        if (result > 0) {
            return myResult;
        }else {
            myResult.setMessage("增加失败");
            myResult.setCode(StateCode.INSERT_FAILURE);
        }
        return myResult;
    }

    /**
     * 上传文件Excel文件
     */
    @PostMapping("/upload")
    public MyResult<List<JobExam>> upload(MultipartFile file, HttpServletRequest request) {

        String id = request.getHeader("id");

        MyResult<List<JobExam>> myResult = new MyResult<>(StateCode.INSERT_SUCCESS, "增加成功");

        List<JobExam> jobExamList = ExcelReader.analysis(file);
        if (jobExamList != null) {
            int result = jobExamServiceImpl.addJobExam(jobExamList, id);
            myResult.setData(jobExamList);
        }else {
            myResult.setCode(StateCode.INSERT_FAILURE);
            myResult.setMessage("上传失败");
        }
        return myResult;
    }
}
